package cse.om.gui;

import cse.om.common.Converter;
import cse.om.common.Utils;
import cse.om.data.Comment;
import cse.om.data.Profile;
import cse.om.object.ProfileAnalyze;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.JLabel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class FormMain extends javax.swing.JFrame {

	public FormMain() {
		initComponents();

		initForm();
		resetFilter();
		resetTimeOption();
	}

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPanel = new javax.swing.JTabbedPane();
        panelProfileList = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableProfile = new javax.swing.JTable();
        btnLoadProfiles = new javax.swing.JButton();
        panelFilter = new javax.swing.JPanel();
        spinnerUpdateFrom = new javax.swing.JSpinner();
        spinnerUpdateTo = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        spinnerCCTo = new javax.swing.JSpinner();
        spinnerCCFrom = new javax.swing.JSpinner();
        chkUpdateFrom = new javax.swing.JCheckBox();
        chkUpdateTo = new javax.swing.JCheckBox();
        chkCCFrom = new javax.swing.JCheckBox();
        chkCCTo = new javax.swing.JCheckBox();
        btnFilter = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        chkTimeFrom = new javax.swing.JCheckBox();
        spinnerTimeFrom = new javax.swing.JSpinner();
        chkTimeTo = new javax.swing.JCheckBox();
        spinnerTimeTo = new javax.swing.JSpinner();
        btnAnalyse = new javax.swing.JButton();
        btnViewComment = new javax.swing.JButton();
        panelPerson = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        lbProfileID = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lbTimeAnalyse = new javax.swing.JLabel();
        lbCommentCount = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        panelSpellChecker = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtSpellChecker = new javax.swing.JTextArea();
        panelTagger = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtTagger = new javax.swing.JTextPane();
        panelSentiWordNet = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtSentiWordNet = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        panelGroup = new javax.swing.JPanel();
        lbTitle = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Opinion Mining");
        setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        setName("frameMain"); // NOI18N

        tabbedPanel.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        panelProfileList.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        jScrollPane1.setBorder(null);
        jScrollPane1.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        tableProfile.setAutoCreateRowSorter(true);
        tableProfile.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 20, 1, 20));
        tableProfile.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        tableProfile.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã người dùng", "Số lượng bình luận", "Lần cập nhật mới nhất"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableProfile.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tableProfile.setColumnSelectionAllowed(true);
        tableProfile.setRowHeight(25);
        tableProfile.setSelectionForeground(new java.awt.Color(0, 0, 0));
        tableProfile.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jScrollPane1.setViewportView(tableProfile);
        tableProfile.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        if (tableProfile.getColumnModel().getColumnCount() > 0) {
            tableProfile.getColumnModel().getColumn(0).setPreferredWidth(150);
            tableProfile.getColumnModel().getColumn(1).setPreferredWidth(120);
            tableProfile.getColumnModel().getColumn(2).setPreferredWidth(250);
        }

        btnLoadProfiles.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        btnLoadProfiles.setText("Hiện danh sách");
        btnLoadProfiles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadProfilesActionPerformed(evt);
            }
        });

        panelFilter.setBorder(javax.swing.BorderFactory.createTitledBorder("Tuỳ chọn lọc"));

        spinnerUpdateFrom.setModel(new javax.swing.SpinnerDateModel());
        spinnerUpdateFrom.setEditor(new javax.swing.JSpinner.DateEditor(spinnerUpdateFrom, "yyyy/MM/dd hh:mm:ss"));

        spinnerUpdateTo.setModel(new javax.swing.SpinnerDateModel());
        spinnerUpdateTo.setEditor(new javax.swing.JSpinner.DateEditor(spinnerUpdateTo, "yyyy/MM/dd hh:mm:ss"));

        jLabel4.setText(">=");

        jLabel6.setText("<=");

        jLabel7.setText("<=");

        jLabel8.setText(">=");

        spinnerCCTo.setModel(new javax.swing.SpinnerNumberModel());

        spinnerCCFrom.setModel(new javax.swing.SpinnerNumberModel());

        chkUpdateFrom.setText("Thời gian cập nhật");

        chkUpdateTo.setText("Thời gian cập nhật");

        chkCCFrom.setText("Số lượng bình luận");

        chkCCTo.setText("Số lượng bình luận");

        btnFilter.setText("Lọc");
        btnFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFilterActionPerformed(evt);
            }
        });

        btnReset.setText("Làm lại");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelFilterLayout = new javax.swing.GroupLayout(panelFilter);
        panelFilter.setLayout(panelFilterLayout);
        panelFilterLayout.setHorizontalGroup(
            panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFilterLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkUpdateFrom)
                    .addComponent(chkUpdateTo)
                    .addComponent(chkCCFrom)
                    .addComponent(chkCCTo))
                .addGap(18, 18, 18)
                .addGroup(panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelFilterLayout.createSequentialGroup()
                        .addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(spinnerUpdateFrom)
                    .addComponent(spinnerUpdateTo)
                    .addComponent(spinnerCCFrom)
                    .addComponent(spinnerCCTo))
                .addContainerGap())
        );
        panelFilterLayout.setVerticalGroup(
            panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFilterLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(chkUpdateFrom)
                    .addComponent(spinnerUpdateFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(spinnerUpdateTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(chkUpdateTo))
                .addGap(18, 18, 18)
                .addGroup(panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(spinnerCCFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(chkCCFrom))
                .addGap(18, 18, 18)
                .addGroup(panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(spinnerCCTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(chkCCTo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setText("Chọn những người cần phân tích:");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Thời gian phân tích"));

        chkTimeFrom.setText("Từ");

        spinnerTimeFrom.setModel(new javax.swing.SpinnerDateModel());
        spinnerTimeFrom.setEditor(new javax.swing.JSpinner.DateEditor(spinnerTimeFrom, "yyyy/MM/dd hh:mm:ss"));

        chkTimeTo.setText("Đến");

        spinnerTimeTo.setModel(new javax.swing.SpinnerDateModel());
        spinnerTimeTo.setEditor(new javax.swing.JSpinner.DateEditor(spinnerTimeTo, "yyyy/MM/dd hh:mm:ss"));

        btnAnalyse.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        btnAnalyse.setText("Phân tích ");
        btnAnalyse.setEnabled(false);
        btnAnalyse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnalyseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnAnalyse, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkTimeFrom)
                            .addComponent(chkTimeTo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(spinnerTimeFrom, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                            .addComponent(spinnerTimeTo))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerTimeFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkTimeFrom))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerTimeTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkTimeTo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAnalyse, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnViewComment.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        btnViewComment.setText("Xem bình luận");
        btnViewComment.setEnabled(false);
        btnViewComment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewCommentActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelProfileListLayout = new javax.swing.GroupLayout(panelProfileList);
        panelProfileList.setLayout(panelProfileListLayout);
        panelProfileListLayout.setHorizontalGroup(
            panelProfileListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelProfileListLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelProfileListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelProfileListLayout.createSequentialGroup()
                        .addGroup(panelProfileListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(panelProfileListLayout.createSequentialGroup()
                                .addComponent(btnLoadProfiles, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnViewComment, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(5, 5, 5))
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelProfileListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panelFilter, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelProfileListLayout.setVerticalGroup(
            panelProfileListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelProfileListLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelProfileListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelProfileListLayout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(panelProfileListLayout.createSequentialGroup()
                        .addComponent(panelFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(11, 11, 11)
                .addGroup(panelProfileListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLoadProfiles, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnViewComment, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        tabbedPanel.addTab("Danh sách người dùng", panelProfileList);

        panelPerson.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N

        jLabel2.setText("Mã người dùng:");

        lbProfileID.setText("Profile ID:");

        jLabel3.setText("Thời gian phân tích:");

        lbTimeAnalyse.setText("Thời gian phân tích");

        lbCommentCount.setText("Số lượng comment");

        jLabel5.setText("Số lượng bình luận:");

        jLabel9.setText("Kết quả phân tích qua các bước:");

        txtSpellChecker.setColumns(20);
        txtSpellChecker.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        txtSpellChecker.setRows(5);
        jScrollPane2.setViewportView(txtSpellChecker);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 726, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelSpellChecker.addTab("Chuẩn hoá", jPanel1);

        txtTagger.setContentType("text/html"); // NOI18N
        txtTagger.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        txtTagger.setText("<html>\r\n  <head>\r\n\r\n  </head>\r\n  <body>\r\n    <p style=\"margin-top: 0 font-family: monospaced\">\r\n      \r\n    </p>\r\n  </body>\r\n</html>\r\n");
        jScrollPane4.setViewportView(txtTagger);

        javax.swing.GroupLayout panelTaggerLayout = new javax.swing.GroupLayout(panelTagger);
        panelTagger.setLayout(panelTaggerLayout);
        panelTaggerLayout.setHorizontalGroup(
            panelTaggerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTaggerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 726, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelTaggerLayout.setVerticalGroup(
            panelTaggerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTaggerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelSpellChecker.addTab("Phân tích từ vựng", panelTagger);

        txtSentiWordNet.setColumns(20);
        txtSentiWordNet.setRows(5);
        jScrollPane3.setViewportView(txtSentiWordNet);

        javax.swing.GroupLayout panelSentiWordNetLayout = new javax.swing.GroupLayout(panelSentiWordNet);
        panelSentiWordNet.setLayout(panelSentiWordNetLayout);
        panelSentiWordNetLayout.setHorizontalGroup(
            panelSentiWordNetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSentiWordNetLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 726, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelSentiWordNetLayout.setVerticalGroup(
            panelSentiWordNetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSentiWordNetLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelSpellChecker.addTab("Phân tích cảm xúc", panelSentiWordNet);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 746, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 387, Short.MAX_VALUE)
        );

        panelSpellChecker.addTab("Hiển thị", jPanel4);

        javax.swing.GroupLayout panelPersonLayout = new javax.swing.GroupLayout(panelPerson);
        panelPerson.setLayout(panelPersonLayout);
        panelPersonLayout.setHorizontalGroup(
            panelPersonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPersonLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPersonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelSpellChecker)
                    .addGroup(panelPersonLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbProfileID, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbCommentCount, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbTimeAnalyse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(panelPersonLayout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        panelPersonLayout.setVerticalGroup(
            panelPersonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPersonLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPersonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbProfileID)
                    .addComponent(jLabel5)
                    .addComponent(lbCommentCount)
                    .addComponent(jLabel3)
                    .addComponent(lbTimeAnalyse))
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelSpellChecker)
                .addContainerGap())
        );

        tabbedPanel.addTab("Phân tích cá nhân", panelPerson);

        javax.swing.GroupLayout panelGroupLayout = new javax.swing.GroupLayout(panelGroup);
        panelGroup.setLayout(panelGroupLayout);
        panelGroupLayout.setHorizontalGroup(
            panelGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 771, Short.MAX_VALUE)
        );
        panelGroupLayout.setVerticalGroup(
            panelGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 494, Short.MAX_VALUE)
        );

        tabbedPanel.addTab("Phân tích nhóm", panelGroup);

        lbTitle.setFont(new java.awt.Font("Palatino Linotype", 3, 18)); // NOI18N
        lbTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbTitle.setText("Phân tích thái độ, tâm lý người dùng mạng xã hội");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tabbedPanel)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabbedPanel)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoadProfilesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadProfilesActionPerformed
		loadTable();
    }//GEN-LAST:event_btnLoadProfilesActionPerformed
	
	private void loadTable() {
		btnAnalyse.setEnabled(false);
		btnAnalyse.setText("Đang xử lí...");

		if (_mapCommentCount == null) {
			_mapCommentCount = Utils.getMapProfileIdWithCommentCount();
		}

		_model = (DefaultTableModel) tableProfile.getModel();
		_model.setRowCount(0);
		Set<Long> ids = _mapCommentCount.keySet();
		for (Long id : ids) {
			Object[] value = _mapCommentCount.get(id);
			_model.addRow(new Object[]{id, value[0],
				Converter.unixTimestamp2DateSimpleFormat((String) value[1])});
		}
		tableProfile.setModel(_model);

		btnAnalyse.setText("Phân tích");
	}
		
    private void btnAnalyseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnalyseActionPerformed
		List<Long> profileIdList = getSelectedIdList();
		switch (profileIdList.size()) {
			case 0:
				return;
			case 1:
				analyseProfile(profileIdList.get(0));
				break;
			default:
				analyseGroup(profileIdList);
		}
    }//GEN-LAST:event_btnAnalyseActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
		resetFilter();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFilterActionPerformed
		updateFilter();
    }//GEN-LAST:event_btnFilterActionPerformed

    private void btnViewCommentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewCommentActionPerformed
        List<Long> profileList = getSelectedIdList();
		if (profileList.isEmpty()) return;
		
		FormViewComment frm = new FormViewComment(
			profileList.get(0),
			getTimeFrom(),
			getTimeTo()
		);
		frm.setVisible(true);
    }//GEN-LAST:event_btnViewCommentActionPerformed

	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Windows".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(FormMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(FormMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(FormMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(FormMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				FormMain frmMain = new FormMain();
				frmMain.setExtendedState(MAXIMIZED_BOTH);
				frmMain.setVisible(true);
				
			}
		});
	}

	private static Map<Profile, List<Comment>> _mapComments;
	private static Map<Long, Object[]> _mapCommentCount;
	private static DefaultTableModel _model;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnalyse;
    private javax.swing.JButton btnFilter;
    private javax.swing.JButton btnLoadProfiles;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnViewComment;
    private javax.swing.JCheckBox chkCCFrom;
    private javax.swing.JCheckBox chkCCTo;
    private javax.swing.JCheckBox chkTimeFrom;
    private javax.swing.JCheckBox chkTimeTo;
    private javax.swing.JCheckBox chkUpdateFrom;
    private javax.swing.JCheckBox chkUpdateTo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel lbCommentCount;
    private javax.swing.JLabel lbProfileID;
    private javax.swing.JLabel lbTimeAnalyse;
    private javax.swing.JLabel lbTitle;
    private javax.swing.JPanel panelFilter;
    private javax.swing.JPanel panelGroup;
    private javax.swing.JPanel panelPerson;
    private javax.swing.JPanel panelProfileList;
    private javax.swing.JPanel panelSentiWordNet;
    private javax.swing.JTabbedPane panelSpellChecker;
    private javax.swing.JPanel panelTagger;
    private javax.swing.JSpinner spinnerCCFrom;
    private javax.swing.JSpinner spinnerCCTo;
    private javax.swing.JSpinner spinnerTimeFrom;
    private javax.swing.JSpinner spinnerTimeTo;
    private javax.swing.JSpinner spinnerUpdateFrom;
    private javax.swing.JSpinner spinnerUpdateTo;
    private javax.swing.JTabbedPane tabbedPanel;
    private javax.swing.JTable tableProfile;
    private javax.swing.JTextArea txtSentiWordNet;
    private javax.swing.JTextArea txtSpellChecker;
    private javax.swing.JTextPane txtTagger;
    // End of variables declaration//GEN-END:variables

	private void initForm() {
		DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
		rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
		tableProfile.getColumnModel().getColumn(2).setCellRenderer(rightRenderer);
		tableProfile.getSelectionModel().addListSelectionListener(new SelectionListener());
	}

	private void resetFilter() {
		chkUpdateFrom.setSelected(false);
		chkUpdateTo.setSelected(false);
		chkCCFrom.setSelected(false);
		chkCCTo.setSelected(false);
		Date date = new Date();
		spinnerUpdateTo.setValue(date);
		date.setTime(date.getTime() - 7 * 24 * 60 * 60 * 1000);
		spinnerUpdateFrom.setValue(date);
		spinnerCCFrom.setValue(0);
		spinnerCCTo.setValue(1000);
	}

	private void updateFilter() {
		int rowCount = tableProfile.getRowCount();
		String timeFrom = Converter.date2SimpleFormat((Date) spinnerUpdateFrom.getValue());
		String timeTo = Converter.date2SimpleFormat(((Date) spinnerUpdateTo.getValue()));
		int countFrom = (int) spinnerCCFrom.getValue();
		int countTo = (int) spinnerCCTo.getValue();
		
		_model = (DefaultTableModel) tableProfile.getModel();
		for (int i = rowCount - 1; i >= 0; --i) {
			int cmCount = (int) _model.getValueAt(i, 1);
			if (chkCCFrom.isSelected() && cmCount < countFrom) {
				_model.removeRow(i);
				continue;
			}
			if (chkCCTo.isSelected() && cmCount > countTo) {
				_model.removeRow(i);
				continue;
			}
			String time = (String) _model.getValueAt(i, 2);
			if (chkUpdateFrom.isSelected() && time.compareTo(timeFrom) < 0) {
				_model.removeRow(i);
				continue;
			}
			if (chkUpdateTo.isSelected()&& time.compareTo(timeTo) > 0) {
				_model.removeRow(i);
			}
		}
	}

	private List<Long> getSelectedIdList() {
		_model = (DefaultTableModel) tableProfile.getModel();
		List<Long> list = new ArrayList<>();
		for (int i : tableProfile.getSelectedRows()) {
			list.add((Long) _model.getValueAt(tableProfile.convertRowIndexToModel(i), 0));
		}
		return list;
	}

	private void resetTimeOption() {
		Date date = new Date();
		spinnerTimeTo.setValue(date);
		date.setTime(date.getTime() - 7 * 24 * 60 * 60 * 1000);
		spinnerTimeFrom.setValue(date);
	}
	
	private Date getTimeFrom() {
		if (chkTimeFrom.isSelected())
			return (Date) spinnerTimeFrom.getValue();
		else
			return new Date(0);
	}
	
	private Date getTimeTo() {
		if (chkTimeTo.isSelected())
			return (Date) spinnerTimeTo.getValue();
		else
			return new Date();
	}

	private void analyseProfile(Long profileId) {
		tabbedPanel.setSelectedComponent(panelPerson);
		lbProfileID.setText(profileId.toString());
		lbCommentCount.setText(_mapCommentCount.get(profileId)[0].toString());
		
		Date timeFrom = getTimeFrom(), timeTo = getTimeTo();
		lbTimeAnalyse.setText("từ " + Converter.date2SimpleFormat(timeFrom) + 
			" đến " + Converter.date2SimpleFormat(timeTo));
		
		ProfileAnalyze pa = new ProfileAnalyze(profileId, timeFrom, timeTo);
		pa.analyse();
		txtSpellChecker.setText(Converter.listToMultilineString(pa.getNormalizedStrings()));
		txtTagger.setText(Converter.listToHtmlString(pa.getTaggedWordTags()));
		txtSentiWordNet.setText(Converter.listSentiToMultilineString(pa.getSensitivityList()));
	}

	private void analyseGroup(List<Long> profileIdList) {
		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}

	private class SelectionListener implements ListSelectionListener {
		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (!e.getValueIsAdjusting()) {
				btnAnalyse.setEnabled(tableProfile.getSelectedRowCount() > 0);
				btnViewComment.setEnabled(tableProfile.getSelectedRowCount() > 0);
			}
		}
	}
}
